// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CustomButton from "../BASE/CButton";
import Character from "../BASE/Character";
import ComponentBase from "../BASE/ComponentBase";
import { gif } from "../BASE/spineANDgif/gif";
import turn from "../game/time/turn";


import GeZiManager from "../Manager/GeZiManager";
import { Message } from "../Message/Message";
import MessageCenter from "../Message/MessageCenter";

import heart from "./heart";

import HP from "./heart";

const {ccclass, property} = cc._decorator;

@ccclass
export default class sideUI extends ComponentBase {


   PC:Character=null;
   MaxHP: number=2;
   HP:number=0;
qi:number=0;
   
SC:number=-1  
   
   @property
    pturn:number=0;
    @property(cc.Prefab)
    heart:cc.Prefab=null;
    
    @property(cc.Prefab)
   
    hearts:HP[]=[];
    @property(cc.SpriteAtlas)
    Atlas:cc.SpriteAtlas=null;


    start () {


GeZiManager.sideUI.push(this);

this.node.getChildByName("TN").getComponent(CustomButton).disableButton()
console.log(GeZiManager.sideUI)
}


setM(){
    
   this.hearts=[];
for(let a of this.node.getChildByName("HPx").children){
    a.destroy();}
   
    this.MaxHP=this.PC.MaxHP;
    this.HP=this.PC.HP;
    this.qi=this.PC.qi;
   
    for(let i=1;i<=this.MaxHP;i++){
      let HP1=cc.instantiate(this.heart)
      HP1.setParent(this.node.getChildByName("HPx"))
   HP1.getComponent(heart).DB=i;
    
   HP1.active=true;
  
   }
 
    for(let h of this.hearts){h.node.scaleX=h.node.scaleY;}
    this.PC.UI.push(this)
    
   this.setHP();
    }


setS(n:number){
    if(this.pturn==n){       
    switch (this.pturn) {
    case 1:this.PC=GeZiManager.P1;
        
        break;
        case 2:this.PC=GeZiManager.P2;
        
        break;
        case 3:this.PC=GeZiManager.P3;
        
        break;
        case 4:this.PC=GeZiManager.P4;
        
        break;
        case 5:this.PC=GeZiManager.P5;
        
        break;
        case 6:this.PC=GeZiManager.P6;
        
        break;
    default:
        break;
}
this.MaxHP=this.PC.MaxHP;
this.HP=this.PC.HP;
this.qi=this.PC.qi;
console.log(this.PC.MaxHP)
console.log(this.PC.Pturn)
for(let i=1;i<=this.MaxHP;i++){
  let HP=cc.instantiate(this.heart)
  HP.setParent(this.node.getChildByName("HPx"))
HP.getComponent(heart).DB=i;
  
console.log(  HP.getComponent(heart).DB)
}

for(let h of this.hearts){h.node.scaleX=h.node.scaleY;}
this.PC.UI.push(this)

this.setQi();


    }}


    setHP(){
        this.MaxHP=this.PC.MaxHP;
        this.HP=this.PC.HP;
    if(this.HP<=0){ this.HP=0}
    
        console.log(this.PC.MaxHP)
    
        console.log(this.PC.HP)
        console.log(this.PC.Pturn)
    
    
        let n=this.MaxHP-this.HP;
    for(let mannger of this.hearts){
        mannger.to1();
        
    }
    
     for(let i=this.MaxHP;i>this.HP;i--){
    for(let mannger of this.hearts){
      if(mannger.DB==i){mannger.to0();}
    
    }
    
    
    
    }
    
    }



    setQi(){

        let a=""+ this.PC.qi;
        this.node.getChildByName("qin").getComponent(cc.Label).string=a

    }
    
    setMaxHP(){this.MaxHP=this.PC.MaxHP;}




    ReceiveMessage(message: Message) {
console.log(this.PC.Pturn!=turn.turn)
   
     this.TN()
     this.TNS()
     
if (message) {
    this.setHP();
    this.setQi();
    this.FU()
}
   for (let a of GeZiManager.sideUI) {
    if (a.PC&&a.PC.Pturn!=turn.turn) {
        a.node.getChildByName("TN").getComponent(CustomButton).disableButton()
    }else{a.node.getChildByName("TN").getComponent(CustomButton).enableButton()}
    
   }  


     }


setEQ(){}

useTN(){

    MessageCenter.MakeGMessage("AM",[GeZiManager.PCP.ZB],GeZiManager.PCP.TNS,GeZiManager.PCP.Pturn,"UTNS");

}

MB(){



}



TN(){

this.node.getChildByName("TN").children[2].getComponent(gif).changeF(GeZiManager.PCP.TN,1)


}
TNS(){


   let a= this.node.getChildByName("TN").children[1]
a.children[0].active=false
a.children[1].active=false
a.children[2].active=false
a.children[3].active=false
a.children[4].active=false
for (let index = 0; index <GeZiManager.PCP.TNS ; index++) {
a.children[index].active=true
    
}

}

FU(){
let d
if (this.PC.newFU!=-1) {
    switch(this.PC.newFU){


        case 0:d="Huo"
           
           break;
           case 1:d="Tu"
           
           break;
           case 2:d="Shui"
           
           break;
     
        default:d="feng"
           break;


}

let a=this.PC.FU[0]+this.PC.FU[1]+this.PC.FU[2]+this.PC.FU[3]
this.node.getChildByName("FU").children[a-1].getComponent(gif).spriteFrame=this.node.getChildByName("FU").children[this.PC.FU.length-3].getComponent(gif).atlas1.getSpriteFrame(d)
this.PC.newFU=-1
}


}


SE(){}
SJ(){}
}
